package com.gzw.shop.http;

import android.content.Context;
import android.content.Intent;

import com.gzw.shop.App;
import com.gzw.shop.activity.LoginActivity;
import com.gzw.shop.utils.ToastUtils;

import java.io.IOException;

import dmax.dialog.SpotsDialog;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by JACKYGUO on 2017-03-07.
 * <p>
 * 访问网络时用来显示隐藏对话框
 */

public abstract class SpotsCallBack<T> extends BaseCallBack<T> {

    private SpotsDialog mDialog;

    private Context context;

    public SpotsCallBack(Context context) {
        this.context = context;
        mDialog = new SpotsDialog(context);
    }

    public void showDialog() {
        mDialog.show();
    }

    public void dismissDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    public void setMessage(String msg) {
        mDialog.setMessage(msg);
    }

    @Override

    public void onRequestBefore(Request request) {
        showDialog();
    }

    @Override
    public void onFailure(Call call, IOException e) {
        ToastUtils.show(context, "访问网络错误信息："+e.getMessage());
        dismissDialog();
    }

    @Override
    public void onResponse(Response response) {
        dismissDialog();
    }

    @Override
    public void onTokenError(Response response, int code) {
        dismissDialog();
        ToastUtils.show(context, "登录失效");
        App.getInstance().clearUser();

        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }
}
